 //
//  NewsViewController.m
//  HWSGW
//
//  Created by avachen on 15/10/31.
//  Copyright © 2015年 luxuanrui. All rights reserved.
//

#import "NewsViewController.h"
#import "StarView.h"
#import "HWDataService.h"
#import "NewsModel.h"
#import "NewsCell.h"
#import "UIImageView+WebCache.h"
#import "UIViewExt.h"
#import "NewsDetailViewController.h"
#import "ImageListController.h"

CGFloat ImageHeight = 150.0;

@interface NewsViewController ()

@end

@implementation NewsViewController
{
    UIImageView *_imgView; //头条图片
    UILabel *_titleLabel; //头条标题
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    //设置背景
//    _tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg_main"]];
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg_main"]];
    _tableView.backgroundColor = [UIColor clearColor];
    
    [self createHeaderView];
    [self requestData];
    
}
- (void)loadHeaderViewData{
    if (_data.count > 0) {
        NewsModel *newsModel = self.data[0];
        NSURL *url = [NSURL URLWithString:newsModel.image];
        [_imgView sd_setImageWithURL:url];
        
        _titleLabel.text = newsModel.title;
        
        
    }
}
- (void)createHeaderView{
    //创建UI
    //图片
    _imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, ImageHeight)];
    _imgView.backgroundColor = [UIColor purpleColor];
    [self.view insertSubview:_imgView belowSubview:_tableView];
    
    //头条标题
    _titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, ImageHeight - 30, kScreenWidth, 30)];
    _titleLabel.backgroundColor = [UIColor colorWithWhite:0 alpha:0.3];
    _titleLabel.font = [UIFont systemFontOfSize:16];
    _titleLabel.textColor = [UIColor whiteColor];
    [self.view insertSubview:_titleLabel belowSubview:_tableView];
    
    
}
- (void)requestData{
    //读取数据
    NSArray *jsonArray = [HWDataService requestData:news_list];
    _data = [[NSMutableArray alloc] initWithCapacity:jsonArray.count];
    
    for (NSDictionary *dic in jsonArray) {
        NewsModel *news = [[NewsModel alloc] init];
        
        news.newsId = [dic objectForKey:@"id"];
        news.type = [dic objectForKey:@"type"];
        news.title = [dic objectForKey:@"title"];
        news.summary = [dic objectForKey:@"summary"];
        news.image = [dic objectForKey:@"image"];
        
        [_data addObject:news];
        
    }
    //刷新tableView
    [_tableView reloadData];
    
    [self loadHeaderViewData];
    
    
}
#pragma mark - UITableView delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _data.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //创建单元格，有2种类型
    if (indexPath.row == 0) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"identifyHeaderNewsCell" forIndexPath:indexPath];
        //在故事版中设置2个控件的tag值，根据tag值来传递imageView和label的数据
        cell.backgroundColor = [UIColor clearColor];
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        return cell;
    }
    //根据故事版中的id来创建单元格
    NewsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"identifyNewsCell" forIndexPath:indexPath];
    cell.news = _data[indexPath.row];
    cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg_main"]];
    
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    //不同的单元格类型设置不同的高度
    if (indexPath.row == 0) {
        return 150;
    }
    return 60;
}
//可以拿到上下移动距离的方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat offsetY =  scrollView.contentOffset.y;
//    NSLog(@"%f", offsetY);
    if (offsetY > 0) {
        _imgView.top = -offsetY * .5;
        
    }else{
        //原宽度/原高度 = (放大的宽度）/ (放大的高度);
        CGFloat height = ABS(offsetY) + ImageHeight;
        CGFloat width = kScreenWidth / ImageHeight * height;
        CGRect frame = CGRectMake(-(width - kScreenWidth) / 2, 0, width, height);
        _imgView.frame = frame;
    }
    _titleLabel.bottom = _imgView.bottom;
    
    
}
//点击单元格掉用
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NewsModel *news = self.data[indexPath.row];
    NSInteger type = [news.type integerValue];
    if (type == 0) { //普通新闻
//        NewsDetailViewController *newsDetail = [[NewsDetailViewController alloc] init];
        
        NewsDetailViewController *newsDetail = [self.storyboard instantiateViewControllerWithIdentifier:@"NewsDetailViewController"];
        newsDetail.hidesBottomBarWhenPushed = YES;
        
////        newsDetail.storyboard
//        NSLog(@"%@", newsDetail.storyboard);
        
        [self.navigationController pushViewController:newsDetail animated:YES];
        
    }else if (type == 1){//图片新闻
        ImageListController *imageList = [self.storyboard instantiateViewControllerWithIdentifier:@"ImageListController"];
        
        imageList.hidesBottomBarWhenPushed = YES;
        
        [self.navigationController pushViewController:imageList animated:YES];

    }else if (type == 2){//视频新闻
        
    }
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
